package com.yuzhi.common.core.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @Description: 表格分页数据对象
 * @Author: sunshine
 * @Date: 2024-02-28
 */

@Data
@NoArgsConstructor
public class PageDataInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 总记录数
     */
    private long totalCount;

    /**
     * 总页数
     */
    private long totalPage;

    /**
     * 列表数据
     */
    private List<T> list;

    /**
     * 分页大小
     */
    private long pageSize;

    /**
     * 当前页数
     */
    private long pageNum;

    /**
     * 分页
     *
     * @param list  列表数据
     * @param totalCount 总记录数
     * @param totalPage 总页数
     */
    public PageDataInfo(List<T> list, long totalCount, long totalPage) {
        this.list = list;
        this.totalCount = totalCount;
        this.totalPage = totalPage;
    }

    public static <T> PageDataInfo<T> build(IPage<T> page) {
        PageDataInfo<T> rspData = new PageDataInfo<>();
        rspData.setList(page.getRecords());
        rspData.setTotalPage(page.getPages());
        rspData.setTotalCount(page.getTotal());
        rspData.setPageSize(page.getSize());
        rspData.setPageNum(page.getCurrent());
        return rspData;
    }

    public static <T> PageDataInfo<T> build(List<T> list) {
        PageDataInfo<T> rspData = new PageDataInfo<>();
        rspData.setList(list);
        rspData.setTotalCount(list.size());
        return rspData;
    }

    public static <T> PageDataInfo<T> build() {
        PageDataInfo<T> rspData = new PageDataInfo<>();
        return rspData;
    }

}
